<template>
  <el-card class="app-container">
    <div v-if="!showDetail">
      <div class="itemTitle">培训管理</div>
      <div class="search">
        <el-form :inline="true" :model="queryParams" ref="queryForm">
          <el-col :span="5">
            <el-form-item label="培训名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入培训名称" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
                <el-option
                  v-for="dict in statusList"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item>
              <el-button type="primary" @click="handleQuery">搜索</el-button>
              <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </div>
      <div class="content">
        <div class="ct-table-only">
          <elTables :table-data="tableData" :columns="tableColumns" :totalLength="total" :currentPage="queryParams.pageNum"
            :handle-current-change="handleCurrentChange" isPagin :curHeight="tableHeight" />
        </div>
      </div>
    </div>
    <Detail v-if='showDetail' :title="title" :isEdit="isEdit" :form="form" @back="handleDetailBack"/>
  </el-card>
</template>

<script>
const defaultQueryParams = {
  pageNum: 1,
  pageSize: 10,
};
import { getToken } from "@/utils/auth";
import { pageTrain, getTrain, delTrain, trainStatus} from "@/api/edu/train";
import Detail from './components/detail.vue'
export default {
  name: "Train",
  components:{ Detail },
  data() {
    return {
      title:'',
      statusList:[
        // {label:'未开始',value:'2'},
        {label:'进行中',value:'3'},
        {label:'已结束',value:'4'},
      ],
      showDetail:false,
      tableHeight: 0,
      isEdit: false,
      dialogFormVisible: false,
      // 遮罩层
      loading: true,
      
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API + "/edu/train/importData",
      },
      tableData: [],
      // 查询参数
      queryParams: {
        ...defaultQueryParams,
        name: null,
        status: null,
        factoryId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        lecturer: [
          { required: true, message: "讲师不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
      },
      tableColumns: [
        { key: "name", title: "培训名称" },
        { key: "lecturer", title: "讲师" },
        { key: "startTime", title: "开始时间" },
        { key: "endTime", title: "结束时间" },
        { key: "status", title: "状态" ,render:(h,params) => {
          return h('el-tag',{
            props:{
              type:params.row.status=='3'?'':'success'
            }
          },
            this.statusList.find(_ => _.value == params.row.status)?.label,
          )
        }},
        {
          title: "操作",
          align: "center",
          fixed: 'right',
          width: 80,
          render: (h, params) => {
            return h("div", [
            h(
                "el-button",
                {
                  props: {
                    type: "text",
                    size: "small",
                  },
                  directives: [
                    {
                      name: "hasPermi",
                      value: ["edu:train:info"],
                    },
                  ],
                  on: {
                    click: () => {
                      this.handleUpdate(params.row, 2)
                    },
                  },
                },
                "查看"
              )
            ]);
          },
        },
      ]

    };
  },
  created() {
    this.getPage();
  },
  mounted() {
    this.$nextTick(() => {
      this.getTableHeight();
    })
  },
  methods: {
    handleDetailBack(){
      this.showDetail=false;
      this.getPage();
    },
    getTableHeight() {
      let height = document.getElementsByClassName('ct-table-only')[0].clientHeight;
      this.tableHeight = height - 40;
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getPage();
    },
    /** 查询培训管理信息列表 */
    getPage() {
      this.loading = true;
      let params={
        ...this.queryParams,
        state:2,
      }
      pageTrain(params).then(response => {
        this.tableData = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        lecturer: null,
        startTime: null,
        endTime: null,
        profiles: null,
        status: "0",
        factoryId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        createDept: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getPage();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.showDetail=true;
      if (type == 1) {
        this.isEdit = true;
        this.title = "编辑培训管理信息";
      } else {
        this.isEdit = false;
        this.title = "查看培训管理信息";
      }
      this.reset();
      const id = row.id || this.ids
      getTrain(id).then(response => {
        this.form = response.data;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        let data
        if (Array.isArray(ids)) {
          data = ids
        } else {
          data = [ids]
        }
        return delTrain(data);
      }).then(() => {
        this.getPage();
        this.msgSuccess("删除成功");
      })
    },
  }
};
</script>
<style lang="scss" scoped>
</style>